package com.jackrain.nea.fc.sc.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.sc.api.OmsScStockQueryCmd;
import com.jackrain.nea.fc.sc.mapper.OmsScStockMapper;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 电商仓库存同步给OMS 全量查询
 *
 * @author kangccong
 * @create 2018/08/21
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "sc")
public class OmsScStockQueryCmdImpl extends CommandAdapter implements OmsScStockQueryCmd {

    @Autowired
    QueryCpUtil queryCpUtil;

    private Logger accesLogger = LoggerFactory.getLogger(Constants.ACCESS_PARAM_LOGGER);

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
//        log.debug("--------------电商仓库存同步到OMS全量传输---start------------");
        log.info("OmsScStockQueryCmdImpl-->map-->{}",map);

        ValueHolder vh = new ValueHolder();

        OmsScStockMapper omsScStockMapper = ApplicationContextHandle.getBean(OmsScStockMapper.class);
        JSONArray jsonArray = new JSONArray();

        //跨cp库查询属于电仓库的店仓ID
        JSONObject whereJs1 = new JSONObject();
        whereJs1.put("ECSTORE", 1);
        JSONArray data = queryCpUtil.queryCPJsonArray("CP_C_STORE", "ID", whereJs1);

        JSONArray idsArray = new JSONArray();
        for (int i = 0; i < data.size(); i++) {
            JSONObject param1 = data.getJSONObject(i);
            idsArray.add(param1.getLong("ID"));
        }
        Object[] storeids = idsArray.toArray();

        //获取并处理库存数据
        List<HashMap> listData = omsScStockMapper.selectScStock(storeids);
        for (HashMap hashMap : listData) {
            JSONObject js = new JSONObject();
            js.put("CP_C_STORE_ECODE", hashMap.get("cp_c_store_ecode"));
            js.put("QTY_STOCK_ABLE", hashMap.get("qty_stock_able"));
            js.put("PS_C_SKU_ECODE", hashMap.get("ps_c_sku_ecode"));
            js.put("MODIFIEDDATE", hashMap.get("modifieddate"));

            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);
        }

//        log.debug("--------------电商仓库存同步到OMS全量传输---end------------" + jsonArray);
        log.debug("OmsScStockQueryCmdImpl--end-->{}",jsonArray);

        vh.put("data", jsonArray);
        vh.put("code", 0);
        vh.put("message", "查询成功！");
        return vh;
    }
}
